function synchroniseViewWithModel() {
	if (model == null) {
		return;
	}

	optionRowsSelector = "#option_tbody tr[id^='option_']";
	voteBoxSelector = "#option_tbody [id^='vote::']";

	optionRowSyncer = new SortedItemSync(optionRowsSelector, addMissingRow);
	voteSyncer = new ItemSync(voteBoxSelector, addMissingVote);

	optionRowSyncer.syncElements(model.options);
	voteSyncer.syncElements(model.votes);
}


function addMissingRow(optionId, caption, sibling) {

	var rowHtml = renderModelOptionRow(optionId, caption);
	if (sibling == null) {
		$('#option_tbody').prepend(rowHtml);
	} else {
		var selector = getIdSelector(sibling);
		$(selector).after(rowHtml);
	}

	if (lastAddedOptionId == optionId) {
		focusAndSelectEditFieldWithId("captionedit_" +optionId);
	}

}

function addMissingVote(voteId, vote) {
	var selector = getIdSelector('votings:' + vote.optionId + ':' + vote.answer);
	var attr = createAttribute('id', voteId);
	$(selector)
	.append(
			'<span '
			+ attr
			+ '>'
			+ renderViewerImage(voteId,
					decodeURIComponent(vote.participantId))
					+ '</span>');
}


function showCaptionEditField(optionId) {
	_showCaptionEditField(optionId);

	var state = wave.getState();
	var oldCaption = state.get(optionId, '');

	currentEditedOptionsOriginalCaptions[optionId] = trim(oldCaption);

	focusAndSelectEditFieldWithId("captionedit_" + optionId);
}

function _showCaptionEditField(optionId) {
	var form = renderCaptionEditForm(optionId);
	var containerSelector = getIdSelector("container_caption_" + optionId);
	$(containerSelector).html(form);
}

function focusAndSelectEditFieldWithId(id) {
	var editFieldSelector = getIdSelector(id);
	$(editFieldSelector).select();
}

function hideCaptionEditField(optionId, newCaption) {
	var containerSelector = getIdSelector("container_caption_" + optionId);
	var newCaptionSpan = renderContainerCellContent(optionId, newCaption);
	$(containerSelector).html(newCaptionSpan);
	delete currentEditedOptionsOriginalCaptions[optionId];
}

function removeVoteSpanFromDOM(voteSpanId) {
	var voteSelector = getIdSelector(voteSpanId);
	$(voteSelector).remove();

}
